<?php
class BusinessAction extends CommonAction {
	
	function _initialize() {
		$this->dao = D('Vcard');
		parent::_initialize();
	}
	function index(){
	   $this->listmap = '1 and v.site_id = '.$this->siteid;
	   parent::_list('v', 'left join __MEMBER__ as m on m.id = v.member_id', 'm.img_url, m.nickname, v.*');
	   $this->display();
	
	}
	public function _list(){
		//计算总页数
		$count=$this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name')->where($this->listmap)->count();
		import ( 'ORG.Util.Page' );
		//实例化分页类
		$p=new Page($count,$this->pagesize);
		$this->list = $this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name, c.name as cate_name')->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
'.$p->listRows)->select();
        $this->pages=$p->show();
	}
	function selectProduct(){
		$this->dao = D('Product'); 
		$this->listmap="1 and n.site_id=".$this->siteid; 
		$this->listorder="sort,id";
		if(isset($_GET['category_id'])&&intval($_GET['category_id'])>0){
			$this->listmap.=" and n.category_id=".intval($_GET['category_id']);
			$this->category_id=intval($_GET['category_id']);
		}
		
		//搜索标题 
		if(isset($_GET['keywords'])&& $_GET['keywords']!=''){ 
			$keywords=$_GET['keywords'];
			$_GET['keywords']=urlencode($_GET['keywords']);
			$this->listmap.=" and n.title like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		$relateids = $this->dao -> where(array('id' => $this->_get('product_id'), 'site_id' => $this->siteid)) -> getField('relateids');
		$this->selectedid = explode(',', $relateids);
		//dump($selectedid);
		$this->categoryList=M('Product_category')->where(array('site_id'=>$this->siteid))->select(); 
        $this->_list();
		$this->display();
	} 
	function getbusiness(){
		
	   $pagesize = 10;
		 
			if($this->_get('pagesize')){
				
			 $pagesize = $this->_get('pagesize');
			
			}
			$map = array( 
			
			  'p.site_id' => $this->siteid,
			
			);
			
			
			
			if($this->_get('keywords')){
				
				$map['o.ordernumber'] = $this->_get('keywords');
			}
			
			 
			if($this->_get('stime') || $this->_get('etime')){
			  
			  $stime = strtotime($this->_get('stime'))+8*3600;
			  $etime = strtotime($this->_get('etime'))+8*3600;
			  
			 if($this->_get('stime')){
				 
				 $map['o.create_time'] = array('gt',$stime);
			  
			 }
			 
			 if($this->_get('etime')){
				 
			  $map['o.create_time'] = array('lt',$etime);
			 
			 }
			 
			 if($this->_get('etime') && $this->_get('stime')){
				 
			  $map['o.create_time'] = array('gt',$stime);
			  $map['o.create_time'] = array('lt',$etime);
			 
			 }
			 
			}
			
			
	    
		   $business_siteDB = M('business_site');
		   $count = $business_siteDB-> alias('b')->join('INNER JOIN __PRODUCT__ as p on p.id = b.product_id')->where($map)->getField('count(distinct(b.b_id)) as tp');
		   //实例化分页类
			import ( 'ORG.Util.Page' );
			$p=new Page($count,$pagesize);
			
			$data = $business_siteDB->alias('b')->join('LEFT JOIN __PRODUCT__ as p on p.id = b.product_id')->where($map)->order('b.b_id DESC')->group("product_id")->limit($p->firstRow.',
	'.$p->listRows) -> select();
			
		   $pages=$p->show(); 
		   $this->assign('list',$data);
		   $this->assign('pages',$pages);
		   $this->display();
	
	}
	
	
	
	
	public function deletebusiness_site(){
		$map['b_id'] = $this->_get('id', 'int');
		if($map['b_id'] > 0){
			$db = M('Business_site');
			$ret = $db -> where($map) -> delete();
			if($ret){
				$this->success('删除成功！');
			}else{
				$this->error($db -> getError());
			}
		}else{
			$this->error('参数错误！');
		}
	}
	
	
	
	
	
	function add(){
	      
		  
		  if(IS_GET){
			  
		     $business_siteDB = M('business_site');
			  
			if($this->_get('type') == 'delete'){
				
			  $mes = $business_siteDB->where(array('b_id'=>$this->_get('id')))->delete();
			  if($mes){
				  $this->success('操作成功',U('getbusiness'));
				  exit; 
				}else{
				  $this->error('操作失败');
				  exit;
				}
			}
		  
		  
		  
		  }
		  
		  
		  if(IS_POST){
			$business_siteDB = M('business_site');
			  
			 
			$data['product_id']=$this->_post('product_id');
			$data['affiliateconfig']=$this->_post('affiliateconfig');
			$data['money']=$this->_post('money');
			$data['site_id']=$this->siteid;
			$mes = $business_siteDB->add($data);
			if($mes){
			  $this->success('新增成功',U('getbusiness'));
			  exit; 
			}else{
			  $this->error('添加失败');
			  exit;
			}
		    
		     
		  }
		  
		  $AffiliateConfig = C('AffiliateConfig');
		  $this->assign('AffiliateConfig',$AffiliateConfig);
	      $this->display();
	
	}
	
	

	
	
}
